package cn.homework.web.servlet;

import cn.homework.domain.Admin;
import cn.homework.domain.Customers;
import cn.homework.service.RegisterService;
import cn.homework.service.impl.RegisterServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

/**
 * @author ACT
 */
@WebServlet("/registerServlet")
public class RegisterServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取需求信息
        String demand = request.getParameter("demand");
        switch (demand) {
            case "customerRegister": {
                commodityCustomerRegister(request, response);
                break;
            }
            case "customerMaterial": {
                commodityCustomerMaterial(request, response);
                break;
            }
            case "backstageRegister": {
                backstageAdminRegister(request, response);
                break;
            }
            default:
                break;
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    /**
     * 前台用户注册信息
     * @param request
     * @param response
     */
    private void commodityCustomerRegister(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取验证码数据
        String verifyCode = request.getParameter("verifyCode");
        //3.验证码校验
        HttpSession session = request.getSession();
        String checkcode_server = (String) session.getAttribute("CHECKCODE_SERVER");
        session.removeAttribute("CHECKCODE_SERVER");
        if (!checkcode_server.equalsIgnoreCase(verifyCode)) {
            //验证码不正确
            //提示信息
            request.setAttribute("verifyCodeError_msg", "验证码错误！");
            //跳转注册页面
            request.getRequestDispatcher("/CommodityRegistered.jsp").forward(request, response);
            return;
        }
        //获取信息
        Map<String, String[]> map = request.getParameterMap();
        //封封装成JavaBean
        Customers customers = new Customers();
        try {
            BeanUtils.populate(customers, map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        //调用service查询是否存在此用户
        RegisterService service = new RegisterServiceImpl();
        Customers customer = service.queryCustomer(customers);
        if (customer == null) {
            //调用service添加到数据库
            service.registerCustomer(customers);
            //页面跳转
            request.setAttribute("customers", customers);
            request.getRequestDispatcher("/CommodityRegistrationSuccess.jsp").forward(request, response);
        } else {
            //提示信息
            request.setAttribute("registerCustomerRepetition_msg", "此用户已存在！");
            //跳转注册页面
            request.getRequestDispatcher("/CommodityRegistered.jsp").forward(request, response);
        }
    }

    /**
     * 前台用户信息修改
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void commodityCustomerMaterial(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name = request.getParameter("name");
        Map<String, String[]> map = request.getParameterMap();
        Customers customers = new Customers();
        try {
            BeanUtils.populate(customers, map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        RegisterService service = new RegisterServiceImpl();
        Customers customer = service.updateCustomer(customers);
        response.sendRedirect(request.getContextPath() + "/CommodityHome.jsp");
    }

    /**
     * 后台用户注册
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void backstageAdminRegister(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取验证码数据
        String verifyCode = request.getParameter("verifyCode");
        //3.验证码校验
        HttpSession session = request.getSession();
        String checkcode_server = (String) session.getAttribute("CHECKCODE_SERVER");
        session.removeAttribute("CHECKCODE_SERVER");
        if (!checkcode_server.equalsIgnoreCase(verifyCode)) {
            //验证码不正确
            //提示信息
            request.setAttribute("verifyCodeError_msg", "验证码错误！");
            //跳转注册页面
            request.getRequestDispatcher("/CommodityRegistered.jsp").forward(request, response);
            return;
        }
        //获取信息
        Map<String, String[]> map = request.getParameterMap();
        //封封装成JavaBean
        Admin admin = new Admin();
        try {
            BeanUtils.populate(admin, map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        //调用service查询是否存在此用户
        RegisterService service = new RegisterServiceImpl();
        Admin user = service.queryAdmin(admin);
        if (user == null) {
            //调用service添加到数据库
            service.registerAdmin(admin);
            //页面跳转
            request.setAttribute("admin", admin);
            request.getRequestDispatcher("/BackstageRegisterSuccess.jsp").forward(request, response);
        } else {
            //提示信息
            request.setAttribute("registerCustomerRepetition_msg", "此用户已存在！");
            //跳转注册页面
            request.getRequestDispatcher("/CommodityRegistered.jsp").forward(request, response);
        }
    }
}
